package com.howie.parnote.util;

import com.howie.parnote.constant.ViewConstant;
import com.howie.parnote.util.spring.SpringContextUtil;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/4/19 0019.
 * @version v1.0.0
 */
@Component
public class FreemarkerRenderUtil {

	@Resource(name = "freemarkerConfig")
	private FreeMarkerConfig freemarkerConfig;

	private static FreemarkerRenderUtil freemarkerRenderUtil;

	@PostConstruct
	public void init() {
		freemarkerRenderUtil = this;
	}


	public static String getHtmlString(ViewConstant view, Map<String, String> templateVars) throws IOException, TemplateException {
		StringBuffer content = new StringBuffer();
		Configuration configuration = freemarkerRenderUtil.freemarkerConfig.getConfiguration();
		content.append(FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(view.getPath()+".ftl"), templateVars));
		return content.toString();
	}

}
